<html lang="en">
<head>
    <title>攻略文章管理</title>
    <#include "../common/header.ftl">
    <script type="text/javascript" src="/js/ckeditor/ckeditor.js"></script>
    <script>
        $(function () {

            //富文本框编辑器
            var ck = CKEDITOR.replace('content', {
                ///图片服务器: 保存之前已经上传过的图片, 可以从此地方获取路径即可
                //图片共享服务 e:/img
                filebrowserBrowseUrl: '/这个服务器就厉害了',
                //图片上传请求路径
                filebrowserUploadUrl: '/images/upload.do'
            });




           $(".changeBtn").click(function () {
               $("#editForm")[0].reset();

               //手动给富文本编辑器赋值
               var json = $(this).data("json");


               $("#editForm img").attr("src","");
                if (json){
                    //二级联动回显
                    //第一级"大攻略回显
                    $("#editForm select[name='strategy.id']").val(json.strategyId);
                    //第二级回显
                    $.get("/strategyCatalog/selectCatalogByStrategyId.do",{strategyId:json.strategyId},function (data) {
                        var html = "";
                        $.each(data,function (index, item) {
                            html += "<option value='"+item.id+"'>"+item.name+"</option>";
                        })
                        $("#catalog").html(html);

                        $("#editForm select[name='catalog.id']").val(json.catalogId);

                        //内容回显
                        $.get("/strategyDetail/getContentByStrategyId.do",{detailId:json.id},function (data) {
                            ck.setData(data.content);
                        });
                    });

                    $("#editForm input[name='id']").val(json.id);
                   $("#editForm input[name='title']").val(json.title);

                   $("#editForm img").attr("src",json.coverUrl);
                   $("#editForm input[name='sequence']").val(json.sequence);
                   $("#editForm select[name='state']").val(json.state + "");
                   $("#editForm input[name='coverUrl']").val(json.coverUrl);
               }
               $("#editModal").modal("show");
           });
           $(".submitBtn").click(function () {

               //如果CKeditor无法自动给textarea赋值,手动调用方法赋值
               var content = ck.getData();
               $("#content").html(content);

               $("#editForm").ajaxSubmit(function (data) {
                   if(data.success){
                       window.location.reload();
                   }else(
                       $.messager.confirm("温馨提示",data.msg)
                   )
               })
           });


           $("#strategy").change(function () {
               var strategyId = $(this).val();
               if(strategyId == -1){
                   $("#catalog").html("<option value='-1'>--请选择--</option>");
                   return;
               }
               $.get("/strategyDetail/getCatalogBystrategyId.do",{strategyId:strategyId},function (data) {

                   var html = "<option value='-1'>--请选择--</option>";
                   $.each(data,function (index, item) {
                       html += "<option value='"+item.id+"'>"+item.name+"</option>"
                   });
                   $("#catalog").html(html);


               });

           });
        });
    </script>
</head>
<body>
<!--左侧菜单回显变量设置-->
<#assign currentMenu="strategyDetail">
<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-2">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询-->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label for="strategyId">关键字:</label>
                    <input type="text" class="form-control" id="keyword" name="keyword" value="${qo.keyword!}"
                           placeholder="请输入标题">
                </div>
                <button id="btn_query" class="btn btn-info"><span class="glyphicon glyphicon-search"></span> 查询</button>
                <button type="button" id="btn_add" class="btn btn-info changeBtn"><span class="glyphicon glyphicon-plus"></span> 添加攻略</button>
            </form>

            <table class="table table-striped table-hover" >
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>标题</th>
                        <th>封面</th>
                        <th>发布时间</th>
                        <th>排序</th>
                        <th>攻略类别</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
               <#list result.list as entity>
                   <tr>
                       <td>${entity_index + 1}</td>
                       <td>${entity.title!}</td>
                       <td><img src="${entity.coverUrl!}" width="40px"></td>
                       <td>${(entity.releaseTime?string("yyyy-MM-dd"))!}</td>
                       <td>${(entity.sequence)!}</td>
                       <td>${(entity.catalog.name)!}</td>
                       <td>${(entity.stateDisplay)!}</td>
                       <td>
                        <a href="javascript:void(0);" class="btn changeBtn" data-json='${entity.jsonString}'>修改</a>
                       </td>
                   </tr>
               </#list>
            </table>
            <#include "../common/page.ftl">
        </div>
    </div>
</div>
<!--攻略文章编辑-->
<div class="modal fade" id="editModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">攻略文章编辑/增加</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm" enctype="multipart/form-data">
                    <input type="hidden" value="" name="id">

                    <div class="form-group" >
                        <label  class="col-sm-3 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"  name="title" value="" placeholder="请输入推荐标题">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img src="" alt="封面" width="200px;">
                            <input id="img" type="file" name="file">
                            <input type="hidden" name="coverUrl">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">所属攻略：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="strategy" name="strategy.id">
                                <option value="-1">--请选择--</option>
                                <#list strategies as p>
                                    <option value="${p.id}">${p.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">攻略类别：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="catalog" name="catalog.id">
                                <option value="-1">--请选择--</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">是否发布：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="state" name="state">
                                <option value="0">否</option>
                                <option value="1">是</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label  class="col-sm-3 control-label">排序：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"  name="sequence" value="">
                        </div>
                    </div>
                    <textarea name="strategyContent.content" id="content" rows="10" cols="80"></textarea>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary submitBtn">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal" >取消</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>